package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mappers.CommentMapper;
import cn.tedu.egweibo1.pojo.dto.CommentDTO;
import cn.tedu.egweibo1.pojo.entyti.Comment;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("insert")
    public int CommentInsert(CommentDTO commentDTO, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null) {
            return 2;
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        commentMapper.select(comment.getUserId());
        commentMapper.insert(commentDTO);
        return 1;
    }
}
